/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.web;


import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.ApiOperation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanhesoft.java.common.core.constant.CommonConstants;
import com.shanhesoft.java.common.core.util.Result;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.core.vo.TreeNode;
import com.shanhesoft.java.common.data.util.QueryWrapperUtil;
import com.shanhesoft.java.common.log.annotation.LogOperate;
import com.shanhesoft.java.common.log.enums.BusinessType;
import com.shanhesoft.java.common.web.resource.BaseResource;
import com.shanhesoft.java.modules.exam.domain.TBankMultipleChoiceQue;
import com.shanhesoft.java.modules.exam.domain.dto.CourseDto;
import com.shanhesoft.java.modules.exam.domain.dto.TBankMultipleChoiceQueDto;
import com.shanhesoft.java.modules.exam.domain.dto.TBankMultipleChoiceQueQueryCriteria;
import com.shanhesoft.java.modules.exam.domain.dto.TQuestionBankQueryCriteria;
import com.shanhesoft.java.modules.exam.service.CourseService;
import com.shanhesoft.java.modules.exam.service.TBankMultipleChoiceQueService;
import com.shanhesoft.java.modules.exam.service.TQuestionBankService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;

import javax.validation.Valid;
import java.util.*;

/**
 * 多选题管理Controller 多选题管理
 * @author admin
 * @version 2020-09-21 10:45:35
 */
@RestController
@RequestMapping(value = "${application.admin-path}/exam/bankMultipleChoiceQue")
@Slf4j
@AllArgsConstructor
public class TBankMultipleChoiceQueResource extends BaseResource {

	private final TBankMultipleChoiceQueService service;

	private final CourseService courseService;

	private final TQuestionBankService tQuestionBankService;

	/**
	 * @param id
	 * @return
	 */
	@GetMapping(CommonConstants.URL_ID_REGEX)
	@PreAuthorize("@pms.hasPermission('exam_tBankMultipleChoiceQue_view')")
	public Result get(@PathVariable String id) {
		log.debug("REST request to get Entity : {}", id);
		return  Result.buildOkData(service.getOneDto(id));
	}
	/**
	 * GET / : get all tBankMultipleChoiceQue.
	 *
	 * @param pm the pagination information
	 * @return the Result with status 200 (OK) and with body all tBankMultipleChoiceQue
	 */

	@PreAuthorize("@pms.hasPermission('exam_tBankMultipleChoiceQue_view')")
	@GetMapping
	@ApiOperation(value = "多选题管理查看")
	public Result getPage(PageModel pm, TBankMultipleChoiceQueQueryCriteria tBankMultipleChoiceQueQueryCriteria) {
		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, tBankMultipleChoiceQueQueryCriteria);
		wrapper.orderByDesc("created_date");
		return Result.buildOkData(service.page(pm, wrapper));
	}

	/**
	 * GET / : get all terms.
	 * 根据课程关联的题库查询被关联的题库及其子题库所有单选题
	 * @return the Result with status 200 (OK) and with body all terms
	 */

	@GetMapping("/getAll")
	@ApiOperation(value = "单选题按课程查询")
	public Result getAllPage(PageModel pm, @RequestParam("courseId") String courseId,
							 @RequestParam("content") String content, @RequestParam("publishFlag") String publishFlag){
		CourseDto courseDto = courseService.getOneDto(courseId);
		String bankId = courseDto.getBankId();
		// 根据题库ID查询当前及其下级ID
		TQuestionBankQueryCriteria tQuestionBankQueryCriteria = new TQuestionBankQueryCriteria();
		tQuestionBankQueryCriteria.setParentId(bankId);
		List<TreeNode> treeNodes = tQuestionBankService.findBankTreeNode(tQuestionBankQueryCriteria);
		Set<String> questionBankIds = new HashSet<String>();
		questionBankIds.add(bankId);
		for(TreeNode node : treeNodes) {
			questionBankIds.add(node.getId());
		}

		TBankMultipleChoiceQueQueryCriteria tBankMultipleChoiceQueQueryCriteria = new TBankMultipleChoiceQueQueryCriteria();
		tBankMultipleChoiceQueQueryCriteria.setQuestionBankIds(questionBankIds);
		if(null != content && !content.equals("")) {
			tBankMultipleChoiceQueQueryCriteria.setContent(content);
		}

		// 只有查已发布题目时需要带该参数，其它情况都是全部可选
		if(StringUtils.isNotEmpty(publishFlag)) {
			if("1".equals(publishFlag)) {
				tBankMultipleChoiceQueQueryCriteria.setPublishFlag(publishFlag);
			}
		}

		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, tBankMultipleChoiceQueQueryCriteria);
		return Result.buildOkData(service.page(pm, wrapper));
	}

	/**
	 * POST / : Save a tBankMultipleChoiceQueDto.
	 *
	 * @param tBankMultipleChoiceQueDto the HTTP tBankMultipleChoiceQue
	 */
	@PreAuthorize("@pms.hasPermission('exam_tBankMultipleChoiceQue_edit')")
	@ApiOperation(value = "多选题管理编辑")
	@PostMapping
	public Result save(@Valid @RequestBody TBankMultipleChoiceQueDto tBankMultipleChoiceQueDto) {
		log.debug("REST request to save TBankMultipleChoiceQueDto : {}", tBankMultipleChoiceQueDto);
		service.saveOrUpdate(tBankMultipleChoiceQueDto);
		return Result.buildOk("保存多选题管理成功");

	}

	/**
	 * DELETE //:ids : delete the "ids" TBankMultipleChoiceQue.
	 *
	 * @param ids the id of the tBankMultipleChoiceQue to delete
	 * @return the Result with status 200 (OK)
	 */
	@PreAuthorize("@pms.hasPermission('exam_tBankMultipleChoiceQue_del')")
	@ApiOperation(value = "多选题管理删除")
	@DeleteMapping
	public Result delete(@RequestBody Set<String> ids) {
		log.debug("REST request to delete TBankMultipleChoiceQue: {}", ids);
		service.removeByIds(ids);
		return Result.buildOk("删除多选题管理成功");
	}

	@ApiOperation(value = "多选题Excel文件上传")
	@PostMapping(value = "/insertList")
	public Result insertList(@RequestBody Map<String, Object> obj){
		List<Map<String, Object>> list = (List<Map<String, Object>>) obj.get("params");
		List<TBankMultipleChoiceQue> bankList = new ArrayList<TBankMultipleChoiceQue>();
		for(Map<String, Object> map : list) {
			TBankMultipleChoiceQue tBankMultipleChoiceQue = new TBankMultipleChoiceQue();
			tBankMultipleChoiceQue.setQuestionBankId((String)map.get("questionBankId"));
			tBankMultipleChoiceQue.setContent((String)map.get("content"));
			tBankMultipleChoiceQue.setAnswer((String)map.get("answer"));
			tBankMultipleChoiceQue.setAnswerExplain((String)map.get("answerExplain"));
			tBankMultipleChoiceQue.setChoiceA((String)map.get("choiceA"));
			tBankMultipleChoiceQue.setChoiceB((String)map.get("choiceB"));
			tBankMultipleChoiceQue.setChoiceC((String)map.get("choiceC"));
			tBankMultipleChoiceQue.setChoiceD((String)map.get("choiceD"));
			tBankMultipleChoiceQue.setChoiceE((String)map.get("choiceE"));
			tBankMultipleChoiceQue.setChoiceF((String)map.get("choiceF"));
			tBankMultipleChoiceQue.setChoiceG((String)map.get("choiceG"));
			bankList.add(tBankMultipleChoiceQue);
		}

		service.saveOrUpdateBatch(bankList);
		return Result.buildOk("文件上传成功");
	}
}
